$(function(){init();});
 function init () {
    // 设置样式
    var filename = '';
    var pathname = window.location.pathname;
    var lastindex = pathname.lastIndexOf('/');
    if ((lastindex + 1) <= pathname.length) {
        filename = pathname.substr(pathname.lastIndexOf('/') + 1);
    }

    var html = [];
    html.push("<ul>");
    html.push("<li id='footerindex'><a>" + getItem("home.html", filename) + "</a></li>");
    html.push("<li id='footermerchant'><a>" + getItem("merchantlist.html", filename) + "</a></li>");
    html.push("<li id='footerbarcodescan'><a class='mid-ma'><img src='../../images/foot_5.png' /> <span>扫一扫</span></a></li>");
    html.push("<li id='footermyinfo'><a>" + getItem("mine.html", filename) + "</a></li>");
    html.push("<li id='footermore'><a>" + getItem("more.html", filename) + "</a></li>");
    html.push("</ul>");

    $("#footer").html(html.join(""));
    $("#footer ul li").on("click", function () {
        var footermenu = $(this);
        var id = footermenu.attr("id");
        switch (id) {
            case "footerindex":
                clicked("../../view/home/home.html");
                break;
            case "footermerchant":
                clicked("../../view/merchant/merchantlist.html");
                break;
            case "footerbarcodescan":
                clicked("../../plus/barcode_scan.html", true, true,"none");
                break;
            case "footermyinfo":
                clicked("../../view/mine/mine.html");
                break;
            case "footermore":
                clicked("../../view/home/more.html");
                break;
            default:
                break;
        }
    });
}

function getItem(tab, current) {
    var item = "";
    switch (tab) {
        case "home.html":
            item = (current == "home.html" ? "<img src='../../images/foot_1_1.png' /> <span style='color:#ff6633'>首页</span>" : "<img src='../../images/foot_1.png' /> <span>首页</span>"); break;
        case "merchantlist.html":
            item = (current == "merchantlist.html" ? "<img src='../../images/foot_2_2.png' /> <span style='color:#ff6633'>商家</span>" : "<img src='../../images/foot_2.png' /> <span>商家</span>"); break;
        case "mine.html":
            item = (current == "mine.html" ? "<img src='../../images/foot_3_3.png' /> <span style='color:#ff6633'>我的</span>" : "<img src='../../images/foot_3.png' /> <span>我的</span>"); break;
        case "more.html":
            item = (current == "more.html" ? "<img src='../../images/foot_4_4.png' /> <span style='color:#ff6633'>更多</span>" : "<img src='../../images/foot_4.png' /> <span>更多</span>"); break;
        default: break;
    }

    return item;
}



// 扫描二维码
function scaned(t, r, f) {
    console.log("扫描结果：" + r);

    // 是否合法URL
    //  if (!r || r.toLowerCase().indexOf("www.o2abc.net") < 0) {
    //      alert("无效二维码！");
    //      return;
    //  }

    // 订单号
    var orderid = getUrlParam("order", r);
    // 获取用户Code
    var scancode = getUrlParam("code", r); //"3EDF" ;
    // 是否用户二维码
    var isInvitebarcodeType = r.toLowerCase().indexOf("login") >= 0;

    // 根据Code获取用信息
    var scanuser = null;
    if (isInvitebarcodeType) {
        $.ajax({
            type: "GET", url: configManager.RequstUrl + "api/user/profile?code=" + scancode,
            async: false
        }).done(function (data) {
            if ("success" != data.state) {
                console.log(data.message);
            }
            scanuser = data.data;
            console.log(JSON.stringify(scanuser));
        });
        if (!scanuser) {
            plus.nativeUI.alert("不存在该用户！"); return;
        }
    }

    var currentloginuser = JSON.parse(plus.storage.getItem(storageManager.user));

    // 只有商家才可以扫描订单码
    // 用户类型 1：普通用户； 2：商家； 3：商家子账号； 4：代理； 5：代理子账号； 6：后台管理人员
    if (!isInvitebarcodeType && currentloginuser && [2, 3].indexOf(currentloginuser.user_type) > -1) {
        plus.nativeUI.alert("对不起，您不是商家不可扫描！"); return;
    }

    // 是否用户二维码
    if (isInvitebarcodeType) {
        // 登录跳转支付/未登录跳转注册
        if (currentloginuser) {
            // 用户自己二维码不允许扫描
            if (currentloginuser.code == scanuser.code) { plus.nativeUI.alert("用户不允许扫描自己的二维码！"); return; }
            var param = "code=" + scancode;
            plus.webview.open("../../view/payment/direct.html?" + param, "../../view/payment/direct.html", {}, "zoom-out", 100, {});
        } else {
            var param = "type=" + scanuser.user_type + "&code=" + scancode;
            plus.webview.open("../../view/account/register.html?" + param, "../../view/account/register.html", {}, "zoom-out", 100, {});
        }

    } else {
        // 登录跳转到订单详情/未登录跳转到登录
        if (currentloginuser) {
            var param = "id=" + orderid;
            plus.webview.open("../../view/mine/order/confirmorder.html?" + param, "../../view/mine/order/confirmorder.html", {}, "zoom-out", 100, {});
        } else {
            // 跳转到登录
            plus.webview.open("../../view/account/login.html", "../../view/account/login.html", {}, "slide-in-bottom", 100, {});
        }
    }
}
